package com.sjgs.gis.gs;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 20170721.
 */
public class StyleParse {

    private static final Log logger = LogFactory.getLog(StyleParse.class);

    private static final Pattern pattern1 = Pattern.compile("\t|\r|\n");
    private static final Pattern pattern2 = Pattern.compile(" & ");
    private static final Pattern pattern3 = Pattern.compile("\\\\r\\\\n|\\\\r\\\\n\\s+");
    private static final Pattern pattern4 = Pattern.compile(">\\s+<sld:");
    private static final Pattern pattern5 = Pattern.compile(">\\s+</sld:");

    public  static String checkStyle(String stylebody){
        if(stylebody.startsWith("<?xml")){
            stylebody = trimSLD(stylebody);
           return  stylebody;
        }
        return null;
    }

    public static String trimSLD(String body){

        Matcher m1 = pattern1.matcher(body);
        if (m1.find()) {
            body = m1.replaceAll("");
        }
        Matcher m2 = pattern2.matcher(body);
        if (m2.find()) {
            body = m2.replaceAll(" &amp; ");
        }

        Matcher m3 = pattern3.matcher(body);
        if(m3!=null){
            body= m3.replaceAll("");
        }

        Matcher m4 = pattern4.matcher(body);
        if(m4!=null){
            body= m4.replaceAll("><sld:");
        }

        Matcher m5 = pattern5.matcher(body);
        if(m5!=null){
            body= m5.replaceAll("></sld:");
        }
        return body;
    }
}
